//
//  ZFMapboxViewController+control.swift
//  map
//
//  Created by fyzq on 2024/2/2.
//

import Foundation
import FloatingPanel
import Hue

extension ZFMapboxViewController{
    
    fileprivate struct MapboxViewKeys {
        static var floatingPanel: Void?
        static var location: Void?
        static var mapViewControl: Void?
        static var weatherView: Void?
        static var szImageView: Void?
        
    }
    
    public var fpc: FloatingPanelController {
        get {
            let vc =  objc_getAssociatedObject(self, &MapboxViewKeys.floatingPanel) as? FloatingPanelController
            return vc!
        }
        set {
            objc_setAssociatedObject(self, &MapboxViewKeys.floatingPanel, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    public var locationControlView: ZFMapLocationControlView {
        get {
            let vc =  objc_getAssociatedObject(self, &MapboxViewKeys.location) as? ZFMapLocationControlView
            return vc!
        }
        set {
            objc_setAssociatedObject(self, &MapboxViewKeys.location, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    private var mapViewControl: ZFMapViewControl {
        get {
            let vc =  objc_getAssociatedObject(self, &MapboxViewKeys.mapViewControl) as? ZFMapViewControl
            return vc!
        }
        set {
            objc_setAssociatedObject(self, &MapboxViewKeys.mapViewControl, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    private var weatherView: ZFWeatherView {
        get {
            let vc =  objc_getAssociatedObject(self, &MapboxViewKeys.weatherView) as? ZFWeatherView
            return vc!
        }
        set {
            objc_setAssociatedObject(self, &MapboxViewKeys.weatherView, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    public var szImageView: UIImageView {
        get {
            let vc =  objc_getAssociatedObject(self, &MapboxViewKeys.szImageView) as? UIImageView
            return vc!
        }
        set {
            objc_setAssociatedObject(self, &MapboxViewKeys.szImageView, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
     func setFloat(){
        fpc = FloatingPanelController()
        fpc.layout = ZFFloatingPanelLayout()
        fpc.delegate = self
        let vc = ZFMenuViewController()
        vc.mapManager = self.mapManager
        fpc.set(contentViewController: vc)
        fpc.track(scrollView: vc.toolScrollView)
        fpc.addPanel(toParent: self)
        fpc.surfaceView.appearance = {
            let appearance = SurfaceAppearance()
            appearance.cornerRadius = 8.0
            appearance.shadows = []
            return appearance
        }()
        fpc.surfaceView.backgroundColor = R.color.color_f1()
        fpc.surfaceView.containerMargins = .init(top: 25, left: 0, bottom: 0, right: 0)
        
        let searchHeader = ZFMenuSearchHeaderView()
        searchHeader.hero.id = "ABC123"
        fpc.surfaceView.grabberHandle.isHidden = true
        fpc.surfaceView.addSubview(searchHeader)
        searchHeader.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(10)
            make.right.equalTo(-10)
        }
         
        
        ///搜索
         searchHeader.jk.addGestureTap {[weak self] _ in
            guard let self = self else {return}
            let vc = ZFSearchViewController()
            vc.modalPresentationStyle = .fullScreen
            self.present(vc, animated: true)
        }
        let maxY = fpc.surfaceLocation(for: .half).y
        addControl(maxY: maxY)
    
    }

}


extension ZFMapboxViewController : FloatingPanelControllerDelegate {
    func floatingPanelDidMove(_ fpc: FloatingPanelController) {
        let loc = fpc.surfaceLocation
        let halfY = fpc.surfaceLocation(for: .half).y
        let y = max(halfY, loc.y)
        locationControlView.snp.updateConstraints { make in
            make.bottom.equalTo(-UIDevice.height + y - 5)
        }

        if loc.y < halfY {
            let progress = max(1 - (halfY - loc.y) / 44.0, 0)
            locationControlView.alpha = progress;
            mapViewControl.alpha = progress
            weatherView.alpha = progress
        } else {
            locationControlView.alpha = 1;
            mapViewControl.alpha = 1
            weatherView.alpha = 1
        }

        let tipY = fpc.surfaceLocation(for: .tip).y
        if loc.y > tipY - 44.0 {
            let progress = max(0.0, min((tipY  - loc.y) / 44.0, 1.0))
            fpc.surfaceView.backgroundColor = R.color.color_f1()!.alpha(progress)
        } else {
            fpc.surfaceView.backgroundColor = R.color.color_f1()!.alpha(1)
        }
    }
    
}


class ZFFloatingPanelLayout :FloatingPanelLayout {
    var position: FloatingPanel.FloatingPanelPosition = .bottom
    var initialState: FloatingPanel.FloatingPanelState = .half
    var anchors: [FloatingPanel.FloatingPanelState : FloatingPanel.FloatingPanelLayoutAnchoring] = [
//        .full: FloatingPanelLayoutAnchor(absoluteInset: 16.0, edge: .top, referenceGuide: .safeArea),
        .half: FloatingPanelLayoutAnchor(fractionalInset: 0.33, edge: .bottom, referenceGuide: .safeArea),
        .tip: FloatingPanelLayoutAnchor(absoluteInset: 52.0, edge: .bottom, referenceGuide: .safeArea),
    ]
}


extension ZFMapboxViewController {
    
    fileprivate func addControl(maxY:CGFloat){
        guard let mapView = mapManager.mapView else {
            return
        }
        
        ///底部
        locationControlView =  ZFMapLocationControlView()
        view.insertSubview(locationControlView,aboveSubview: mapView)
        locationControlView.snp.makeConstraints { make in
            make.right.equalTo(-10)
            make.bottom.equalTo(-UIDevice.height + maxY - UIDevice.statusBarHeight)
        }
        
        ///头部
        mapViewControl = ZFMapViewControl()
        view.insertSubview(mapViewControl,aboveSubview: mapView)
        mapViewControl.snp.makeConstraints { make in
            make.right.equalTo(-10)
            make.top.equalTo(UIDevice.safeAreaInsets.top + 10)
        }
        
        ///天气
        weatherView = ZFWeatherView()
        view.insertSubview(weatherView,aboveSubview: mapView)
        weatherView.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.bottom.equalTo(locationControlView.snp.bottom)
        }
        weatherView.loadNowWeather()
        
        ///十字
        szImageView = UIImageView()
        szImageView.image =  R.image.ic_home_sz()
        view.insertSubview(szImageView,aboveSubview: mapView)
        szImageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(30)
        }
        
        ///图层
        mapViewControl.layerLabel.jk.addGestureTap { _ in
            UIView.feedback()
            let layerView = ZFLayerView()
            ZFSwiftMessage.show(view: layerView,presentationStyle: .bottom)
            layerView.mapLayerView.sublayerView.subject.subscribe(onNext: {[weak self] modal in
                guard let self = self else {return}
                self.mapManager.setStyleUrl(url:  modal.layerUrl!)
            }).disposed(by: layerView.dis)
            
            layerView.mapSettingView.subject.subscribe(onNext: {[weak self]idx in
                guard let self = self else {return}
                if idx == 5 {
                    
                }else {
                    self.onMapConfig()
                }
            }).disposed(by: layerView.dis)

        }
        
        ///客服
        mapViewControl.kefuLabel.jk.addGestureTap {[weak self] _ in
            guard let self = self else {return}
            UIView.feedback()
            
            let vc =  ZFScnViewController()
            self.navigationController?.pushViewController(vc, animated: true)
            
        }
        
        ///定位
        locationControlView.locationLabel.jk.addGestureTap {[weak self]_ in
            guard let self = self else {return}
            UIView.feedback()
            self.mapManager.location()
        }
        locationControlView.pitchView.jk.addGestureTap {[weak self]_ in
            guard let self = self else {return}
            UIView.feedback()
            ///配置信息
            guard let config = ZFMapConfigModal.getMapConfig(),
            config.enablePitch else {
                return
            }
            
            self.mapManager.changePitch()
        }
      
    }
    
    
    
    
    
}

